package nthu.web.model.business.persistence;

import javax.servlet.ServletException;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nthu.web.model.domain.box.MemoBox;


@SuppressWarnings("serial")
public class UserBoxDao extends ResourceDao<MemoBox> {


    @Override
	public void doGet (HttpServletRequest request, HttpServletResponse response)
                                                 throws ServletException, IOException {
/*   趨勢科技
		String databaseIP = "192.168.200.228";
		String databasePort = "30285";
	 	String userName = "nthu";
		String userPassword = "nthu987";
		String targetDataBase ="pipezon";
		String jdbcURL = "jdbc:mysql://" + databaseIP + ":" + databasePort + "/" + targetDataBase + "?useUnicode=true&amp;characterEncoding=UTF-8";
*/
    	String databaseIP = "localhost";
    	String userName = "essweb";
    	String userPassword = "essess";
    	String targetDataBase ="pipezon";
    	String jdbcURL = "jdbc:mysql://" + databaseIP  + "/" + targetDataBase + "?useUnicode=true&amp;characterEncoding=UTF-8";

        try {
        	Class.forName("com.mysql.jdbc.Driver");
        	Connection conn = DriverManager.getConnection(jdbcURL, userName, userPassword);
            Statement stmt = conn.createStatement();

            ArrayList arraylist = new ArrayList();
            ResultSet result = stmt.executeQuery("SELECT * FROM memobox");
            // Fetch each row from the result set

            while (result.next()) {
            	Long id = result.getLong("id");
                Long ctime = result.getLong("ctime");
                Long cid = result.getLong("cid");
                String name = result.getString("name");
                String content = result.getString("content");

                MemoBox box = new  MemoBox(id,cid ,name ,content);
                box.setStamp(ctime);  //System.currentTimeMillis()

                arraylist.add(box);
            }
            
            result.close();
            stmt.close();
            conn.close();
           
            setModel(request,arraylist);


        }catch(ClassNotFoundException ex) {
        	System.out.println("找不到驅動程式類別\n");
        }catch(SQLException ex) {
           System.out.print("SQL Exception : " + ex +"\n");
        }
	}


}

